package com.dddpeter.app.service;

import com.dddpeter.app.entity.Article;
import com.dddpeter.app.entity.ArticleLike;
import com.dddpeter.app.repository.ArticleLikeMapper;
import com.dddpeter.app.repository.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 文章点赞服务类
 */
@Service
public class ArticleLikeService {
    
    @Autowired
    private ArticleLikeMapper articleLikeMapper;
    
    /**
     * 点赞文章
     */
    @Transactional
    public boolean likeArticle(Long articleId, String nickname) {
        // 检查是否已经点赞
        int exists = articleLikeMapper.checkLikeExists(articleId, nickname);
        if (exists > 0) {
            return false; // 已经点赞过了
        }
        
        // 添加点赞记录
        ArticleLike like = new ArticleLike();
        like.setArticleId(articleId);
        like.setNickname(nickname);
        like.setCreatedAt(LocalDateTime.now());
        articleLikeMapper.insert(like);
        
        // 更新文章点赞数（暂时不实现，因为Article实体中没有likeCount字段）
        
        return true;
    }
    
    /**
     * 检查用户是否已点赞文章
     */
    public boolean hasLikedArticle(Long articleId, String nickname) {
        return articleLikeMapper.checkLikeExists(articleId, nickname) > 0;
    }
}
